package com.jinlejia.erp.ui.home;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.ViewPagerAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.bean.WorkGuideBean;
import com.jinlejia.erp.databinding.ActivityWorkDetailsBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class WorkDetailsActivity extends AppCompatActivity {


    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments;
    //Tab选项卡的文字
    private String mTitleArray[] = {"经管中心", "交易中心", "财务中心", "营销中心", "人力行政", "员工成长中心","新房电商中心","售前装修","战略发展中心","董事长办公室", "IT中心"};
    private ViewPagerAdapter viewPagerAdapter;
    WorksFragment manageMentFragment = new WorksFragment();
    ArrayList<WorkGuideBean> manageMentList;
    WorksFragment dealFragment = new WorksFragment();
    ArrayList<WorkGuideBean> dealList;
    WorksFragment financeFragment = new WorksFragment();
    ArrayList<WorkGuideBean> financeList;
    WorksFragment marketingFragment = new WorksFragment();
    ArrayList<WorkGuideBean> marketingList;
    WorksFragment hrFragment = new WorksFragment();
    ArrayList<WorkGuideBean> hrList;
    WorksFragment trainFragment = new WorksFragment();
    ArrayList<WorkGuideBean> trainList;
    WorksFragment newhouseFragment = new WorksFragment();//新房电商中心
    ArrayList<WorkGuideBean> newhouseList;
    WorksFragment customerserviceFragment = new WorksFragment();//售前装修
    ArrayList<WorkGuideBean> customerserviceList;
    WorksFragment zhanlueFragment = new WorksFragment();//战略发展中心
    ArrayList<WorkGuideBean> zhanlueList;
    WorksFragment bossFragment = new WorksFragment();//董事长办公室
    ArrayList<WorkGuideBean> bossList;
    WorksFragment itFragment = new WorksFragment();
    ArrayList<WorkGuideBean> itList;

    private ActivityWorkDetailsBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_work_details);
        initView();
        getData();
    }

    private void initView() {
        mFragments = new ArrayList<>();
        mFragments.add(manageMentFragment);
        mFragments.add(dealFragment);
        mFragments.add(financeFragment);
        mFragments.add(marketingFragment);
        mFragments.add(hrFragment);
        mFragments.add(trainFragment);
        mFragments.add(newhouseFragment);
        mFragments.add(customerserviceFragment);
        mFragments.add(zhanlueFragment);
        mFragments.add(bossFragment);
        mFragments.add(itFragment);


        viewPagerAdapter = new ViewPagerAdapter(this.getSupportFragmentManager(), mTitleArray, mFragments);
        binding.viewPager.setAdapter(viewPagerAdapter);
        binding.viewPager.setOffscreenPageLimit(10);
        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.idTableLayout.setTabsFromPagerAdapter(viewPagerAdapter);
        binding.idTableLayout.post(() -> {
            // 将TabLayout和ViewPager进行关联，让两者联动起来
            binding.idTableLayout.setupWithViewPager(binding.viewPager);
        });

        findViewById(R.id.back_btn).setOnClickListener(v -> finish());
    }

    private void getData() {
        OkHttpUtils
                .postString()
                .url(ApiConfig.WORKGUIDE)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content("")
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(WorkDetailsActivity.this, response)) {

                            JSONObject jsonObject = JSON.parseObject(response.get("data").toString());
                            if (jsonObject.containsKey("manageMent")) {
                                manageMentList = JSON.parseObject(jsonObject.getJSONArray("manageMent").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                manageMentFragment.setData(manageMentList);
                            }
                            if (jsonObject.containsKey("deal")) {
                                dealList = JSON.parseObject(jsonObject.getJSONArray("deal").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                dealFragment.setData(dealList);
                            }
                            if (jsonObject.containsKey("finance")) {
                                financeList = JSON.parseObject(jsonObject.getJSONArray("finance").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                financeFragment.setData(financeList);
                            }
                            if (jsonObject.containsKey("marketing")) {
                                marketingList = JSON.parseObject(jsonObject.getJSONArray("marketing").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                marketingFragment.setData(marketingList);
                            }
                            if (jsonObject.containsKey("humanResources")) {
                                hrList = JSON.parseObject(jsonObject.getJSONArray("humanResources").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                hrFragment.setData(hrList);
                            }
                            if (jsonObject.containsKey("train")) {
                                trainList = JSON.parseObject(jsonObject.getJSONArray("train").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                trainFragment.setData(trainList);
                            }

                            if (jsonObject.containsKey("newhouse")) {
                                newhouseList = JSON.parseObject(jsonObject.getJSONArray("newhouse").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                newhouseFragment.setData(newhouseList);
                            }

                            if (jsonObject.containsKey("customerservice")) {
                                customerserviceList = JSON.parseObject(jsonObject.getJSONArray("customerservice").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                customerserviceFragment.setData(customerserviceList);
                            }

                            if (jsonObject.containsKey("zhanlue")) {
                                zhanlueList = JSON.parseObject(jsonObject.getJSONArray("zhanlue").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                zhanlueFragment.setData(zhanlueList);
                            }

                            if (jsonObject.containsKey("dongshizhangbangongshi")) {
                                bossList = JSON.parseObject(jsonObject.getJSONArray("dongshizhangbangongshi").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                bossFragment.setData(bossList);
                            }

                            if (jsonObject.containsKey("it")) {
                                itList = JSON.parseObject(jsonObject.getJSONArray("it").toJSONString(), new TypeReference<ArrayList<WorkGuideBean>>() {
                                });
                                itFragment.setData(itList);
                            }
                        }
                    }
                });
    }

}
